package wangjg.xuexiban.activity.category;

import java.io.File;
import java.util.List;

import wangjg.commons.io.file.FileIOHelper;
import wangjg.xuexiban.R;
import wangjg.xuexiban.activity.BasicActivity;
import wangjg.xuexiban.activity.card.CardPagerActivity;
import wangjg.xuexiban.entity.Category;
import wangjg.xuexiban.util.Constant;
import wangjg.xuexiban.util.FileDownCallback;
import wangjg.xuexiban.util.FileDownCallback.DownloadVo;
import wangjg.xuexiban.util.JsonUtil;
import wangjg.xuexiban.util.WebLoader;
import wangjg.xuexiban.vo.JsonResponse;
import wangjg.xuexiban.vo.PageData;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public abstract class BasicCategoryActivity extends BasicActivity {
	protected Context context;
	private List<Category> categoryList;
	
	protected List<Category> getCategoryList(){
		if(categoryList==null){
			this.loadCategoryList();
		}
		return categoryList;
	}
	
	protected void loadCategoryList(){
		FileDownCallback ic = new FileDownCallback(this.handler, FileDownCallback.TYPE_DATA);
		WebLoader.instance().loadData(Constant.URL_CATEGORY_DOWN, ic);
	}

	public BasicCategoryActivity() {
		this.context = this;
	}

	protected View getCategoryView(Category category) {
		String subject = category.getName();
		View view = this.inflate(R.layout.category_item);
		ImageView category_img = (ImageView) view.findViewById(R.id.category_photo);
		
		if(category.getPhoto()!=null && category.getPhoto().length()>0){
			FileDownCallback ic = new FileDownCallback(this.handler, FileDownCallback.TYPE_IMG, category_img);
			WebLoader.instance().loadFile(Constant.picUrl(category.getPhoto()), ic);	
		}

		TextView textView = (TextView) view.findViewById(R.id.category_text);
		textView.setText(subject);
		view.setOnClickListener(new CL(category.getUuid()));
		view.setTag(category);
		return view;
	}
	
	protected void initContentView() {
		
	}

	@SuppressLint("HandlerLeak")
	protected final Handler handler = new Handler(){
        @Override  
        public synchronized void handleMessage(Message msg) {
        	DownloadVo dv = (DownloadVo)msg.obj;
        	String filePath = dv.filePath;
        	ImageView img = (ImageView) dv.obj;

        	int type = msg.what;
        	
        	Log.d("handleMessage", dv.toString());
        	
        	File file = new File(filePath);
        	boolean fileExists = file.exists() && file.isFile();
        	
        	if(WebLoader.Callback.STATUS_FAILURE.equals(dv.status) && (!fileExists || type==FileDownCallback.TYPE_DATA)){
        		Toast.makeText(context, "加载网路"+FileDownCallback.getTypeName(type)+"失败!", Toast.LENGTH_SHORT).show();
        		return;
        	}
        	
        	if(type==FileDownCallback.TYPE_AUDIO){
        		
        	}else if(type==FileDownCallback.TYPE_IMG){
    			if (file.exists() && file.isFile()) {
    				Bitmap bitmap = rc.get(file.getPath());
    				img.setImageBitmap(bitmap);
    			}
        	}else if(type==FileDownCallback.TYPE_DATA){
        		String json = FileIOHelper.FileToString(file, "utf-8");
        		JsonResponse response = JsonUtil.getCgListResp(json);
        		if(response.noError()){
        			PageData<Category> pageData = (PageData<Category>)response.getData();
        			categoryList = pageData.getList();
        			initContentView();
        		}else{
        			Toast.makeText(context, "数据错误:"+response.getMessage(), Toast.LENGTH_SHORT).show();
        		}
        	}
        }
    };

	class CL implements OnClickListener{
		private String uuid;
		
		public CL(String uuid) {
			this.uuid = uuid;
		}

		public void onClick(View view) {
			Intent intent = new Intent(context, CardPagerActivity.class);
			intent.putExtra("category_path", uuid);
			startActivity(intent);
		}
		
	}
}
